import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class ModiuSystem {
    static List<User> users = new ArrayList<>();
    static List<Business> businesses = new ArrayList<>();


    static List<Film> films = new ArrayList<>();

    static Scanner scanner = new Scanner(System.in);

    static User loginUser;
    static Business loginBusiness;


    public static void main(String[] args) {
        films.add(new Film("战狼",1,120,"18:00-20:00",new LinkedList<>(),49));
        films.add(new Film("无双",1,100,"6:00-7:00",new LinkedList<>(),29));
        films.add(new Film("喜剧",1,90,"12:00-15:00",new LinkedList<>(),79));
        while (true) {
            System.out.println("======电影首页======");
            System.out.println("1.登录");
            System.out.println("2.用户注册");
            System.out.println("3.商家注册");
            System.out.println("4.退出系统");
            System.out.println("==================");
            System.out.println("请输入操作命令:");
            int select = scanner.nextInt();
            switch (select) {
                case 1:
                    login();
                    break;
                case 2:
                    userRegister();
                    break;
                case 3:
                    businessRegister();
                    break;
                case 4:
                    System.exit(0);
                    break;
            }
        }
    }

    static void login() {
        //3表示未找到
        int flag = 3;
        System.out.println("请输入账号:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        for (User user :
                users) {
            if (user.userName.equals(username)) {
                flag = 1;
                if (user.password.equals(password)) {
                    loginUser = user;
                    System.out.println("登陆成功");
                    index(user);
                } else {
                    System.out.println("密码错误");
                    return;
                }
            }
        }

        for (Business business :
                businesses) {

            if (business.businessName.equals(username)) {
                flag = 2;
                if (business.password.equals(password)) {
                    System.out.println("登陆成功");
                    loginBusiness = business;
                    index(business);
                } else {
                    System.out.println("密码错误");
                    return;
                }
            }

        }
        if (flag == 3) {
            System.out.println("用户名不存在");
        }

    }

    static void userRegister() {
        System.out.println("请输入账号:");
        String userName = scanner.next();
        System.out.println("请输入姓名");
        String name = scanner.next();
        System.out.println("请输入性别");
        String sex = scanner.next();
        System.out.println("请输入手机号码");
        String phone = scanner.next();
        System.out.println("请输入账户密码");
        String password = scanner.next();
        System.out.println("请输入存储金额");
        double money = scanner.nextDouble();
        User user = new User(userName, name, sex, phone, password, money,new Purchase());
        users.add(user);
        System.out.println("用户注册成功");
    }

    static void businessRegister() {
        System.out.println("请输入账号:");
        String userName = scanner.next();
        System.out.println("请输入姓名");
        String name = scanner.next();
        System.out.println("请输入性别");
        String sex = scanner.next();
        System.out.println("请输入手机号码");
        String phone = scanner.next();
        System.out.println("请输入账户密码");
        String password = scanner.next();
        System.out.println("请输入存储金额");
        double money = scanner.nextDouble();
        System.out.println("请输入店名");
        String storeName = scanner.next();
        System.out.println("请输入地铺地址");
        String address = scanner.next();
        Business business = new Business(userName, name, sex, phone,
                password, money, storeName, address);
        businesses.add(business);
        System.out.println("商家注册成功");
    }

    static void index(User user1) {
            System.out.println("===========用户界面===========");
        int flag = 1;
        while (true) {

            if (flag == 2) {
                break;
            }
            System.out.println("============================");
            System.out.println("         1.购票");
            System.out.println("         2.显示当前购买的票");
            System.out.println("         3.充值");
            System.out.println("         4.评分");
            System.out.println("         5.全部影片信息");
            System.out.println("         6.查询指定电影信息");
            System.out.println("         7.查看余额");
            System.out.println("         8.退出用户模式");
            System.out.println("============================");
            System.out.println("请输入你的选择:");

            int select = scanner.nextInt();
            switch (select) {
                case 1:
                    pur();
                    break;
                case 2:
                    showPur();
                    break;
                case 3:
                    Recharge();
                    break;
                case 4:
                    score();
                    break;
                case 5:
                    showFilmes();
                    break;
                case 6:
                    selectFilm();
                    break;
                case 7:
                    showMoney();
                    break;
                case 8:
                    flag = 2;
                    break;
            }
        }


    }

    static void pur() {
        showFilmes();
        System.out.println("请输入要购买的电影名");
        String name = scanner.next();
        for (Film film :
                films) {

            if (film.filmName.equals(name)) {
                if (film.price > loginUser.money) {
                    System.out.println("余额不足,请充值");
                    return;
                }

                loginUser.purchase.films.add(film);
                loginUser.money -= film.price;
                System.out.println("购买成功");
                return;
            }

        }
        System.out.println("未找到要买的电影");
    }

    static void showPur() {

        if (loginUser.purchase.films.size()==0){
            System.out.println("未购买票!");
            return;
        }
        for (Film film :
                loginUser.purchase.films) {
            System.out.println(film);
        }
    }

    static void showFilmes() {
        for (Film film :
                films) {

            if (film.flag == 1) {
                System.out.println(film);
            }

        }
    }

    static void Recharge() {
        System.out.println("亲输入要充值的金额:");
        double money = scanner.nextInt();
        loginUser.money += money;
        System.out.println("充值成功");
    }

    static void score() {
        for (Film film :
                films) {
            System.out.println(film);
        }
        System.out.println("情输入要评分的电影名");
        String name = scanner.next();
        for (Film film :
                films) {
            if (film.filmName.equals(name)) {
                System.out.println("请输入你的评分:");
                int fen = scanner.nextInt();
                film.pingFen.add(fen);
                System.out.println("评分成功");
                return;
            }
        }
        System.out.println("未找到该电影");
    }

    static void selectFilm() {
        for (Film film :
                films) {
            if (film.flag == 1) {
                System.out.print("电影名:" + film.filmName+" ");
            }

        }
        System.out.println("请输入查询的电影名");
        String name = scanner.next();
        for (Film film :
                films) {
            if (film.filmName.equals(name)) {
                System.out.println(film);
            }

        }
    }

    static void showMoney(){
        System.out.println("当前余额为:"+loginUser.money);
    }

    static void index(Business business1) {
        System.out.println("===========商家界面===========");
        int flag = 1;
        while (true) {

            if (flag == 2) {
                break;
            }
            System.out.println("============================");
            System.out.println("      1.目前上映的影片信息");
            System.out.println("      2.上架影片");
            System.out.println("      3.下架影片");
            System.out.println("      4.修改电影信息");
            System.out.println("      5.退出商家模式");
            System.out.println("============================");
            System.out.println("请输入你的选择:");

            int select = scanner.nextInt();
            switch (select) {
                case 1:
                    showUpFilmes();
                    break;
                case 2:
                    UpFilmes();
                    break;
                case 3:
                    DownFilmes();
                    break;
                case 4:
                    updateFilmes();
                    break;
                case 5:
                    flag = 2;
                    break;
            }
        }
    }

    private static void updateFilmes() {
        showUpFilmes();
        System.out.println("请输入要修改的电影名");
        String name = scanner.next();
        for (Film film :
                films) {
            if (film.filmName.equals(name)){
                System.out.println("请输入电影时长:");
                int duration = scanner.nextInt();
                System.out.println("请输入电影播放时间:");
                String time = scanner.next();
                System.out.println("请输入电影价格:");
                double price = scanner.nextDouble();

                film.duration = duration;
                film.time = time;
                film.price = price;
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("未找到该电影");

    }

    static void UpFilmes() {
        System.out.println("请输入上架的电影名:");
        String name = scanner.next();
        for (Film film :
                films) {
            if(film.filmName == name){
                film.flag = 1;
                return;
            }
        }
        int flag = 1;
        System.out.println("请输入上架电影的时长:");
        int duration = scanner.nextInt();
        System.out.println("请输入电影播放的时间:");
        String time = scanner.next();
        System.out.println("请输入电影的票价:");
        double price = scanner.nextDouble();
        Film film = new Film(name, flag, duration, time, new ArrayList<>(), price);
        films.add(film);
        System.out.println("上架成功");

    }
    static void  DownFilmes(){
        System.out.println("请输入下架的电影名:");
        String name = scanner.next();
        for (Film film :
                films) {
            if(film.filmName.equals(name)){
                film.flag = 2;
                System.out.println("下架成功");
                return;
            }
        }
        System.out.println("要下架的电影不存在");
    }

    static void showUpFilmes(){
        if(films.size() == 0){
            System.out.println("暂无电影");
            return;
        }
        for (Film film :
                films) {
            if(film.flag == 1){
                System.out.println(film);
            }
        }
    }
}
